import bcrypt from 'bcryptjs';
import connectDB from '../lib/mongodb';
import User from '../models/User';

async function createAdminUser() {
  try {
    await connectDB();

    const adminUsername = 'admin';
    const adminPassword = 'admin123';

    // 检查管理员用户是否已存在
    const existingAdmin = await User.findOne({ username: adminUsername });
    
    if (existingAdmin) {
      console.log('管理员账号已存在');
      process.exit(0);
    }

    // 创建新的管理员用户
    const hashedPassword = await bcrypt.hash(adminPassword, 12);
    
    const adminUser = await User.create({
      username: adminUsername,
      password: hashedPassword,
      avatar: '/default-avatar.png',
    });

    console.log('adminUser is:', adminUser);
    console.log('管理员账号创建成功：');
    console.log('用户名：admin');
    console.log('密码：admin123');
    
    process.exit(0);
  } catch (error) {
    console.error('创建管理员账号失败:', error);
    process.exit(1);
  }
}

createAdminUser(); 